package com.hwadee.ems.controller;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;

import com.hwadee.ems.domain.dto.PageQuery;
import com.hwadee.ems.domain.dto.Result;
import com.hwadee.ems.domain.po.ClassroomApplication;
import com.hwadee.ems.domain.vo.ClassroomApplicationVO;
import com.hwadee.ems.service.IClassroomApplicationService;

/**
 * <p>
 * 教室借用申请 前端控制器
 * </p>
 *
 * @author EnosElinsa
 * @since 2024/06/21
 */
@Slf4j
@RestController
@RequestMapping("/classroom_application")
public class ClassroomApplicationController {

    @Resource
    private IClassroomApplicationService classroomApplicationService;

    @GetMapping("/page")
    public Result<IPage<ClassroomApplicationVO>> page(
        PageQuery pageQuery,
        @RequestParam(required = false) String applicantName,
        @RequestParam(required = false) String approverName,
        @RequestParam(required = false) Integer approvalStatus,
        @RequestParam(required = false) Long semesterId,
        @RequestParam(required = false) Integer week
    ) {
        return classroomApplicationService.page(pageQuery, 
            applicantName, 
            approverName, 
            approvalStatus, 
            semesterId, 
            week
        );
    }

    @PostMapping
    public Result<Long> save(@RequestBody ClassroomApplication classroomApplication) {
        log.info("save classroomApplication: " + classroomApplication);
        return classroomApplicationService.saveWithApproval(classroomApplication);
    }

    @PutMapping
    public Result<Void> updateById(@RequestBody ClassroomApplication classroomApplication) {
        if (classroomApplicationService.updateById(classroomApplication)) {
            log.info("update classroomApplication: " + classroomApplication);
            return Result.ok();
        } else {
            log.error("update classroomApplication failed: " + classroomApplication);
            return Result.error();
        }
    }
    
    @DeleteMapping("/{id}")
    public Result<Void> removeById(@PathVariable Long id) {
        if (classroomApplicationService.removeById(id)) {
            log.info("remove classroomApplication by id: " + id);
            return Result.ok();
        } else {
            log.error("remove classroomApplication by id failed: " + id);
            return Result.error();
        }
    }

    @GetMapping("/page_by_applicant_id")
    public Result<IPage<ClassroomApplicationVO>> pageByApplicantId(PageQuery pageQuery,
        @RequestParam Long applicantId, 
        @RequestParam(required = false) String approverName,
        @RequestParam(required = false) Integer approvalStatus,
        @RequestParam(required = false) Long semesterId,
        @RequestParam(required = false) Integer week) {
        return classroomApplicationService.pageByApplicantId(applicantId, pageQuery, 
            approverName, approvalStatus, semesterId, week);
    }

}